<?php
/*
 * 手机客户端店铺-商品列表接口
 * 2013-02-24
 * 2014-05-08 (已查) 
 */
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数


$reJson = array(); //构建返回值

/*
 * 常量
 */

$img_Path = "http://www.chinalaohu.com/modules/miniature.php?w=150&h=150&p=/uploads/shop/productImg/";

$platform		= $_REQUEST["platform"];		//platform  0安卓 1苹果
$groupFilter 	= $_REQUEST["groupFilter"];		//过滤父类别，如：品牌分类, 类别分类, 规格
$childFilter 	= $_REQUEST["childFilter"];		//过滤子类别，如：钲艺廊, 稻房
$page			= $_REQUEST["page"]; 			//页数，第一页为0
$pagesize		= $_REQUEST["pagesize"];		//每页记录数

$title			= $_REQUEST["title"];			//标题过滤
$shopId			= $_REQUEST["shopId"];			//店铺主键
$category		= $_REQUEST["category"];		//商品分类
$collegeId		= $_REQUEST["collegeId"];
$showSF			= $_REQUEST["showSF"];			//是否返回过滤,排序信息,  1:返回; 0:不返回
$collection		= $_REQUEST["collection"];

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
$uid = $uInfoArr["uid"];

$groupFilterArr = explode(",",$groupFilter);	//转换为数组  sort
$childFilterArr = explode(",",$childFilter);	//转换为数组

if(count($groupFilterArr)!=count($childFilterArr)){
	echo json_encode(array("success"=>"0","msg"=>"groupFilter与childFilter数量不等"));
	die;
}

if(!isset($page)){
	$page = 0;
}
if(!isset($pagesize)){
	$pagesize = 15; //默认每页15条
}
$business_type = 0;
if ($uid){
$sql2 = "SELECT business_type FROM user_base where uid='".$uid."'";
$db2->query($sql2);
if ($db2->next_record()){
	$business_type = $db2->f("business_type");
}
}
$sql2 = "select name from shop_product_category where category_id='".$category."'";
$db2->query($sql2);
if ($db2->next_record()){
	$category_name = $db2->f("name");
}
if($collegeId){
$sql2 = "select b.shop_id from (select uid from new_weituo_person where xuexiao_id='".$collegeId."') a 
              inner join 
							(select e.uid,e.shop_id from shop_info d inner join (select DISTINCT uid,shop_id from shop_product where shangjia='1') e on d.shop_id=e.shop_id
													    where d.renzhen_flag='1') b on a.uid=b.uid";
$shop_idArr = $db->queryArray($sql2);
$listCount = count($shop_idArr); //记录数
$shop_ids = "";
for($i=0;$i<$listCount;$i++){
	if($i>0){
		$shop_ids = $shop_ids.",";
	}
	$shop_ids = $shop_ids.$shop_idArr[$i][0];
}
if(!$shop_ids){
	$shop_ids = "0";
}
}
if($collection==1){
$sql2 = "SELECT product_id from shop_product_sc where uid='".$uid."'";
$pd_idArr = $db->queryArray($sql2);
$listCount = count($pd_idArr); //记录数
$pd_ids = "";
for($i=0;$i<$listCount;$i++){
	if($i>0){
		$pd_ids = $pd_ids.",";
	}
	$pd_ids = $pd_ids.$pd_idArr[$i][0];
}
if(!$pd_ids){
	$pd_ids = "0";
}
}

//获取符合条件的展品id
$sql = "select product_id from shop_product where shangjia='1'";
//标题名过滤
if($title){
	$sql = $sql." and shop_product.product_name like '%".$title."%'";
}
//店铺过滤
if($shopId){
	$sql = $sql." and shop_product.shop_id='".$shopId."'";
}
//分类过滤
if($category){
	$sql = $sql." and shop_product.category='".$category_name."'";
}
//学校过滤
if($collegeId){
	$sql = $sql." and shop_product.shop_id in (".$shop_ids.")";
}
//收藏过滤
if($collection==1){
	$sql = $sql." and shop_product.product_id in (".$pd_ids.")";
}
$sortValue = ""; //排序类型值
if(count($groupFilterArr)>0){
	for($i=0;$i<count($groupFilterArr);$i++){
		if(!$groupFilterArr[$i] || !$childFilterArr[$i]){
			continue;
		}
		if($groupFilterArr[$i]=="sprice"){
			$priceArr = explode("|",$childFilterArr[$i]);
			$sql = $sql." and price>=".$priceArr[0]." and price<".$priceArr[1];
		}else if($groupFilterArr[$i]=="sort"){
			//获取排序标识
			$sortValue = $childFilterArr[$i];
		}else if($groupFilterArr[$i]=="category"){
			$sql2 = "select name from shop_product_category where category_id='".$childFilterArr[$i]."'";
			$db2->query($sql2);
			if ($db2->next_record()){
				$category_name = $db2->f("name");
			}
			$sql = $sql." and ".$groupFilterArr[$i]."='".$category_name."'";
		}else if($groupFilterArr[$i]=="material"){
			$sql2 = "select material_name from shop_product_material where material_id='".$childFilterArr[$i]."'";
			$db2->query($sql2);
			if ($db2->next_record()){
				$material_name = $db2->f("material_name");
			}
			$sql = $sql." and ".$groupFilterArr[$i]."='".$material_name."'";
		}else{
			$sql = $sql." and ".$groupFilterArr[$i]."='".$childFilterArr[$i]."'";
		}
	}
}
//处理排序
$orderBy = "";
switch ($sortValue){
	case "2":
		//价格由高到低
		$orderBy = "price desc";
		break;
	case "3":
		//价格由低到高
		$orderBy = "price asc";
		break;
	default:
		$orderBy = "product_id desc";
		break;
}

//查询并获取aid数组
$product_idArr = $db->queryArray($sql);
$listCount = count($product_idArr); //记录数
$product_ids = "";
for($i=0;$i<$listCount;$i++){
	if($i>0){
		$product_ids = $product_ids.",";
	}
	$product_ids = $product_ids.$product_idArr[$i][0];
}
if(!$product_ids){
	$product_ids = "0";
}
$sql = "
	select
		product_id,
		image,
		product_name,
		price,
		qyprice,
		color,
		size,
		shop_id
	from
		shop_product 
	where 
		 product_id in (".$product_ids.")
";
if($orderBy){
	$sql = $sql." order by ".$orderBy.",product_id desc";
}
$sql = $sql." limit ".($page * $pagesize).",".$pagesize;

//查询指定展品
$productArr = $db->queryArray($sql);


$reJson["success"] 	= "1";

//获取用户信息
//$uInfoArr = user_getInfoFromUUID();
//if(!$uInfoArr){
//	$reJson["isPersonal"] 	= "1";
//}else{
//$uid = $uInfoArr["uid"];//用户信息主键
//$sql = "select business_type from user_base where uid='".$uid."'";
//$db->query($sql);
//if ($db->next_record()){
//	$business_type = $db->f("business_type");
//}
//if ($business_type==0){
//	$reJson["isPersonal"] 	= "1";
//}else{
//	$reJson["isPersonal"] 	= "0";
//}
//}


if($showSF && intval($showSF,10)==1){
	$filter 		= array();
	$classchild 	= array();
	$brandchild	= array();
	
	if(!$category){
	/*  分类  */
	
		$sql = "select category_id,name from shop_product_category order by category_id asc";
		$db->query($sql);
		while($db->next_record()){
			$classchild[] = array(
				"name"=>$db->f("name")
				,"value"=>$db->f("category_id")
			);
		}
		if(count($classchild)>0){
			$filter[] = array(
							"name"=>"分类"
							,"value"=>"category"
							,"child"=>$classchild
						);
		}
	
	}
	
	/* 材质 */
	$sql = "select material_id,material_name from shop_product_material order by material_id asc";
	$db->query($sql);
	$chizhichild = array();
	while($db->next_record()){
		$chizhichild[] = array(
			"name"=>$db->f("material_name")
			,"value"=>$db->f("material_id")
		);
	}
	if(count($chizhichild)>0){
		$filter[] = array(
			"name"=>"材质"
			,"value"=>"material"
			,"child"=>$chizhichild
		);
	}
	
	/* 价格 */
	$filter[]  = array(
		 "name"=>"价格区间"
		,"value"=>"sprice"
		,"child"=>array(
			 array("name"=>"0-50元","value"=>"0|50")
			,array("name"=>"50-100元","value"=>"50|100")
			,array("name"=>"100-500元","value"=>"100|500")
			,array("name"=>"500-1000元","value"=>"500|1000")
			,array("name"=>"1000-2000元","value"=>"1000|2000")
			,array("name"=>"2000-5000元","value"=>"2000|5000")
			,array("name"=>"5000元以上","value"=>"5000|999999999")
		)
	);
	
	/* 排序 */
	$filter[]  = array(
		 "name"=>"排序"
		,"value"=>"sort"
		,"child"=>array(
			 array("name"=>"默认","value"=>"0")
			,array("name"=>"价格从低到高","value"=>"3")
			,array("name"=>"价格从高到低","value"=>"2")
		)
	);
	
	/* 放入过滤器 */
	$reJson["filter"] = $filter;
}

$reJson["count"] = $listCount;
$sql2 = "SELECT banner_path from shop_store_app_banner where shop_id='".$shopId."'";
$db2->query($sql2);
if ($db2->next_record()){
	$banner = $db2->f("banner_path");
}
$reJson["banner"] ="";
if ($banner){
$reJson["banner"] = "http://www.chinalaohu.com/img1/uploads/shop/appbanner/".$banner;
}
$reJson["serverTime"] = date('Y-m-d');

//循环处理展品记录
$list = array();
for($i=0;$i<count($productArr);$i++){
	if ($productArr[$i]["shop_id"]=='81'  or $productArr[$i]["shop_id"]=='87' or $productArr[$i]["shop_id"]=='88' or $productArr[$i]["shop_id"]=='92' or $productArr[$i]["shop_id"]=='93' and date('Y-m-d')<'2014-04-16'){
		$isBooking = "1";
		$status = "预定中";
	}else{
		$isBooking = "";
		$status = "";
	}
	if ($business_type==1 and $productArr[$i]["qyprice"] and $productArr[$i]["qyprice"]<$productArr[$i]["price"]){
	$listjson = array(
		 "goodsId"=>$productArr[$i]["product_id"]
		,"imgUrl"=>$img_Path.$productArr[$i]["image"]."&aa=1"
		,"name"=>$productArr[$i]["product_name"]
		,"origPrice"=>$productArr[$i]["price"]
		,"currPrice"=>$productArr[$i]["qyprice"]
		,"isBooking"=>$isBooking
		,"status"=>$status
		,"description"=>"颜色：".$productArr[$i]["color"]."\n"."尺寸：".$productArr[$i]["size"]
	);
	}else{
	$listjson = array(
		 "goodsId"=>$productArr[$i]["product_id"]
		,"imgUrl"=>$img_Path.$productArr[$i]["image"]."&aa=1"
		,"name"=>$productArr[$i]["product_name"]
		,"currPrice"=>$productArr[$i]["price"]
		,"isBooking"=>$isBooking
		,"status"=>$status
		,"description"=>"颜色：".$productArr[$i]["color"]."\n"."尺寸：".$productArr[$i]["size"]
	);
	}
	$list[] = $listjson;
}
$reJson["list"] = $list;
echo json_encode($reJson);
die;
	
/**
 * 判断字符串开头是否为指定字符
 * $str 待判断字符串
 * $part 指定字符
 */
function startsWith($str,$part){
	//若第一个字符串长度小于第二个的时候，必须指定返回false，
	//否则substr_compare遇到这种情况会返回0（即相当，与事实不符合）
	if(strlen($str)<strlen($str)){
		return false;
	}else{
		////当第一个长度大于第二个时才开始判断
		return !substr_compare($str,$part,0,strlen($part));
	}
}
?>
